package com.djy.dao;

import com.djy.model.Hotel;

/**
 * @description: 酒店接口实现类
 * @author: djy
 * @data: 2023/8/2 9:51
 * @version: V1.0
 **/
public class HotelDaoImpl implements HotelDao {
    public static Hotel[] hotels = null;
    //= new Hotel[ConstanceUtil.CAPACITYSIZE];
    public static int index=0;
    static{
        Hotel hotel01 = new Hotel(
                (index++)+1,
                "格林豪泰苏州火车站南广场拙政园商务酒店",
                "江苏省苏州市姑苏区人民路2156号",
                new String[]{"大床房","标间","豪华双床房"},
                new double[]{189,169,209},
                new double[]{161,149,190},
                new int[]{20,10,30}
        );
        Hotel hotel02 = new Hotel(
                (index++)+1,
                "格雅苏州工业园区金鸡湖东方之门酒店",
                "江苏省苏州工业园区星海街169号",
                new String[]{"豪华双床房"},
                new double[]{399},
                new double[]{339},
                new int[]{20}
        );

        hotels = new Hotel[]{hotel01,hotel02};
    }

    @Override
    public Hotel[] getHotelList() {
        return hotels;
    }

    @Override
    public int addHotel(Hotel hotel) {
        int addIndex = index++;
        int addCount = 0;
        if(hotel!=null&&hotel.getId()>0){//证明有数据可以添加
            //数组扩容前,复制原来的数据
            if(addIndex>=hotels.length){
                hotels=growHotels(hotels);
            }
            //System.out.println("没有添加酒店之前:"+ Arrays.toString(hotels));
            hotels[addIndex]=hotel;
            //System.out.println("添加酒店之后:"+ Arrays.toString(hotels));
            addCount=1;
        }
        return addCount;
    }

    @Override
    public int deleteHotel(int hotelId) {
        int deleteIndex = -1;
        boolean isHotel = false;
        for(int i=0;i<hotels.length;i++){
            if(hotels[i].getId()==hotelId){
                isHotel=true;
                deleteIndex=i;
                break;
            }
        }
        if(deleteIndex!=-1){//可以删除，把当前索引之前的数据保留，之后数据移动
            //old数据数据
            Hotel[] oldHotel = hotels;//先备份数组
            Hotel[] newHotel = new Hotel[hotels.length];
            System.out.println("deleteIndex="+deleteIndex);
            System.arraycopy(oldHotel,0,newHotel,0,deleteIndex);
            //System.out.println("第一次arraycopy="+Arrays.toString(newHotel));
            int twoCopyLength=hotels.length-(deleteIndex+1);
            //System.out.println("twoCopyLength"+twoCopyLength);
            System.arraycopy(oldHotel,deleteIndex+1,newHotel,deleteIndex,twoCopyLength);
            //System.out.println("第二次arraycopy="+Arrays.toString(newHotel));
            hotels=newHotel;//赋给最大权限范围的数组
        }
        return deleteIndex;
    }

    /**
     * 扩容数组
     * @param paramHotels
     * @return
     */
    private Hotel[] growHotels(Hotel[] paramHotels) {
        int length=paramHotels.length;
        hotels=new Hotel[length*2];
        System.arraycopy(paramHotels,0,hotels,0,length);
        return hotels;
    }
}